package com.beagle.njddapp.ui.fragment;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beagle.njddapp.R;
import com.beagle.njddapp.base.BaseMvpFragment;
import com.beagle.njddapp.bean.oil.HandleBean;
import com.beagle.njddapp.bean.oil.OilHomeBean;
import com.beagle.njddapp.core.OilOrderService;
import com.beagle.njddapp.event.OilChangeEvent;
import com.beagle.njddapp.event.OilOrderEvent;
import com.beagle.njddapp.event.SkEnent;
import com.beagle.njddapp.ui.activity.oil.OilOrderDetailActivity;
import com.beagle.njddapp.ui.adapter.OilHomeOrderAdapter;
import com.beagle.njddapp.ui.adapter.inter.RecyclerViewItemClickLisenter;
import com.beagle.njddapp.ui.presenter.OilHomePresenter;
import com.beagle.njddapp.ui.view.OilHomeView;
import com.beagle.njddapp.weight.dialog.OilDIalog;
import com.beagle.njddapp.weight.dialog.ShouKuanDIalog;
import com.blankj.utilcode.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;

/**
 * @author hxx
 * @date 2021-03-15 19:29
 * 加油车驾驶员 首页 接单列表
 */
public class OilHomeFragment extends BaseMvpFragment<OilHomeView, OilHomePresenter> implements OilHomeView, RecyclerViewItemClickLisenter<OilHomeBean>, OilHomeOrderAdapter.OilOrderHanldeCallBack, OilDIalog.OilDialogHandleCallback {

    @BindView(R.id.common_recyclerview)
    RecyclerView recyclerView;
    @BindView(R.id.smartrefreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.ll_empty)
    LinearLayout llEmpty;
    private OilHomeOrderAdapter adapter;
    private List<OilHomeBean> oilOrderBeanList;
    private int orderId = 0;
    private Intent intent;
    private boolean isStart;
    private OilDIalog oilDIalog;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_recyclerview;
    }

    @Override
    public void init() {
        EventBus.getDefault().register(this);
        intent = new Intent(getContext(), OilOrderService.class);
        getContext().startService(intent);
        isStart = true;
        oilOrderBeanList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new OilHomeOrderAdapter(oilOrderBeanList, getActivity());
        adapter.setOilHomeBeanRecyclerViewItemClickLisenter(this);
        adapter.setOilOrderHanldeCallBack(this);
        recyclerView.setAdapter(adapter);
        refreshLayout.setEnableRefresh(true);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                if (orderId != 0) {
                    presenter.getOrderDetail(orderId);
                } else {
                    refreshLayout.finishRefresh();
                }
            }
        });
    }

    @Override
    public OilHomeView getMvpView() {
        return this;
    }

    @Override
    public OilHomePresenter createPresenter() {
        return new OilHomePresenter();
    }

    @Override
    public void onError(String message) {
        refreshLayout.finishRefresh();
//        if (!TextUtils.isEmpty(message)) {
            ToastUtils.showShort(message);
//        }
    }


    @Override
    public void onItemClickLisenter(OilHomeBean oilHomeBean) {
        startActivity(new Intent(getContext(), OilOrderDetailActivity.class).putExtra("orderId", oilHomeBean.getOrderId()));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        if (isStart) {
            isStart = false;
            getContext().stopService(intent);
        }
    }

    @Subscribe
    public void onEvent(OilOrderEvent event) {
        List<OilHomeBean> handleBeans = event.handleBeans;
        if(handleBeans==null||handleBeans.size()==0){
            llEmpty.setVisibility(View.VISIBLE);
            oilOrderBeanList.clear();
            adapter.notifyDataSetChanged();
            return;
        }
        llEmpty.setVisibility(View.GONE);
        if ( handleBeans.size()==1) {
            presenter.getOrderDetail(handleBeans.get(0).getOrderId());
        } else{
            orderId=0;
            oilOrderBeanList.clear();
            oilOrderBeanList.addAll(handleBeans);
            adapter.notifyDataSetChanged();
        }
    }

    @Subscribe
    public void onEvent(OilChangeEvent event) {
        oilOrderBeanList.clear();
        adapter.notifyDataSetChanged();
        if (event.isChecked) {
            orderId = 0;
            isStart = true;
            getContext().startService(intent);
        } else {
            if (isStart) {
                isStart = false;
                getContext().stopService(intent);
            }
        }
    }

    @Subscribe
    public void onEvent(SkEnent event) {
        isStart = true;
        getContext().startService(intent);
        orderId = 0;
        oilOrderBeanList.clear();
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showOrder(OilHomeBean result) {
        llEmpty.setVisibility(View.GONE);
        refreshLayout.finishRefresh();
        if (result.isPayed()||"finished".equals(result.getOrderState())){
            return;
        }
        oilOrderBeanList.clear();
        oilOrderBeanList.add(result);
        adapter.notifyDataSetChanged();
        if(!"waiting".equals(result.getOrderState())){
            orderId = result.getOrderId();
            if (isStart) {
                isStart = false;
                getContext().stopService(intent);
            }
        }
    }

    @Override
    public void showArrived() {
        ToastUtils.showShort("您已到达，请开始加油");
        if (oilOrderBeanList.size() > 0) {
            oilOrderBeanList.get(0).setOrderState("arrived");
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showSubmit() {
        ToastUtils.showShort("您已完成，请等待加油代发起人付款");
        isStart = true;
        oilOrderBeanList.clear();
        adapter.notifyDataSetChanged();
        getContext().startService(intent);
    }

    @Override
    public void shoukuan() {
        ToastUtils.showShort("收款成功");
        if (oilOrderBeanList.size() > 0) {
            oilOrderBeanList.get(0).setPayed(true);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void showJiedan(int out_orderId) {
        orderId=out_orderId;
        List<OilHomeBean> temp=new ArrayList<>(oilOrderBeanList);
        oilOrderBeanList.clear();
        for(int i=0;i<temp.size();i++){
            if(temp.get(i).getOrderId()==orderId){
                temp.get(i).setOrderState("confirmed");
                oilOrderBeanList.add(temp.get(i));
                break;
            }
        }
        temp.clear();
        adapter.notifyDataSetChanged();
        if (isStart) {
            isStart = false;
            getContext().stopService(intent);
        }
    }
    @Override
    public void handle(OilHomeBean oilHomeBean) {
        if (oilHomeBean == null) {
            return;
        }
        if (oilHomeBean.isPayed()) {
            return;
        }
        if ("waiting".equals(oilHomeBean.getOrderState())) {
            presenter.confirmed(oilHomeBean.getOrderId());
        }else if ("confirmed".equals(oilHomeBean.getOrderState())) {
            presenter.arrived(oilHomeBean.getOrderId());
        } else if ("arrived".equals(oilHomeBean.getOrderState())) {
            if (oilDIalog==null){
                oilDIalog=new OilDIalog(getContext());
                oilDIalog.setOilDialogHandleCallback(this);
            }
            oilDIalog.show();
            oilDIalog.setOrderId(oilHomeBean.getOrderId());
        }
    }


    @Override
    public void oilOk(int orderId, String count) {
        presenter.submit(orderId,count);

    }
}
